package com.gdcp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gdcp.beans.Log;
import com.gdcp.beans.User;
import com.gdcp.common.LogLevelEnum;
import com.gdcp.common.LogTypeEnum;
import com.gdcp.common.PlatformEnum;
import com.gdcp.config.RequestHolder;
import com.gdcp.mapper.LogMapper;
import com.gdcp.service.LogService;
import com.gdcp.util.RequestUtil;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.Date;
import java.util.List;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper sysLogMapper;

    @Override
    public List<Log> findPageBreakByCondition(Log vo) {
        /*PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<SysLog> list = sysLogMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<Log> boList = new ArrayList<>();
        for (SysLog sysLog : list) {
            boList.add(new Log(sysLog));
        }
        PageInfo bean = new PageInfo<SysLog>(list);
        bean.setList(boList);
        return bean;*/
        return null;
    }

    @Async
    @Override
    public void asyncSaveSystemLog(PlatformEnum platform, String bussinessName) {
        String ua = RequestUtil.getUa();
        Log sysLog = new Log();
        sysLog.setLogLevel(String.valueOf(LogLevelEnum.INFO));
        sysLog.setType(String.valueOf(platform.equals(PlatformEnum.WEB) ? LogTypeEnum.VISIT : LogTypeEnum.SYSTEM));
        sysLog.setIp(RequestUtil.getIp());
        sysLog.setReferer(RequestUtil.getReferer());
        sysLog.setRequestUrl(RequestUtil.getRequestUrl());
        sysLog.setUa(ua);
        //sysLog.setSpiderType(WebSpiderUtils.parseUa(ua));
        sysLog.setSpiderType(String.valueOf(0));
        sysLog.setParams(JSONObject.toJSONString(RequestUtil.getParametersMap()));
        User user = (User) RequestHolder.getSession("user");
        if (user != null) {
            sysLog.setUserId(Long.valueOf(user.getId()));
            sysLog.setContent(String.format("用户: [%s] | 操作: %s", user.getUsername(), bussinessName));
        } else {
            sysLog.setContent(String.format("访客: [%s] | 操作: %s", sysLog.getIp(), bussinessName));
        }

        try {
            UserAgent agent = UserAgent.parseUserAgentString(ua);
            sysLog.setBrowser(agent.getBrowser().getName());
            sysLog.setOs(agent.getOperatingSystem().getName());
            this.insert(sysLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Log insert(Log log) {
        Assert.notNull(log, "Log不可为空！");
        log.setUpdate_time(new Date());
        log.setCreate_time(new Date());
        sysLogMapper.insertSelective(log);
        return log;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Integer primaryKey) {
        return sysLogMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSelective(Log log) {
        Assert.notNull(log, "Log不可为空！");
        log.setUpdate_time(new Date());
        return sysLogMapper.updateByPrimaryKeySelective(log) > 0;
    }

    @Override
    public Log getByPrimaryKey(Integer primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        Log log = sysLogMapper.selectByPrimaryKey(primaryKey);
        return log;
    }
}
